﻿using Gs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DemoReception
{
    /// <summary>
    /// RoomSetting.xaml 的交互逻辑
    /// </summary>
    public partial class RoomSetting : Window
    {
        private GsToast iToast;
        private int iViewType;
        private Int64 iRoomTypeId;
        private string iHotelCode;
        private List<KeyValuePair<Int64, string>> iRoomTypeList;
        public RoomSetting(string aCode,int aViewType)
        {
            iRoomTypeId = 0;
            iHotelCode = aCode;
            iViewType = aViewType;
            InitializeComponent();
            iToast = new GsToast();
            iToast.BindControl(this.Content as Grid);
            this.Loaded += new RoutedEventHandler(RoomSetting_Loaded);
        }

        public void RoomSetting_Loaded(object sender, RoutedEventArgs e)
        {
            if(iViewType==0)
            {
                cbType.Visibility = Visibility.Hidden;
                lbType.Visibility = Visibility.Hidden;
                lbRoom.Content = "房型描述";
            }
            else
            {
                cbType.Visibility = Visibility.Visible;
                lbType.Visibility = Visibility.Visible;
                lbRoom.Content = "房间号";
            }
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                int Rst = CHotelController.GetHotelRoomType(out iRoomTypeList,iHotelCode);
                if (Rst < 0)
                {
                    iToast.Show("获取房间分类失败!");
                }
                if (iRoomTypeList != null)
                {
                    InitHotelRoomType();
                }
            }));
        }

        public void SetRoomType(Int64 aId, string aName)
        {
            iRoomTypeId=aId;
            if(iViewType==0)
            {
                tbText.Text = aName;
                NewBtn.Content = "编辑";
            }
        }

        private void InitHotelRoomType()
        {
            cbType.ItemsSource = null;
            cbType.ItemsSource = iRoomTypeList;
            cbType.DisplayMemberPath = "Value";
            cbType.SelectedValuePath = "Key";
            if (iRoomTypeList.Count > 0)
            {
                cbType.SelectedIndex = 0;
            }
        }

        private void NewBtn_Click(object sender, RoutedEventArgs e)
        {
            string ContentText = tbText.Text.Trim();
            if (string.IsNullOrEmpty(ContentText))
            {
                iToast.Show("请填入完整信息!");
                return;
            }

            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                int Rst = 0;
                if(iViewType==0)
                {
                    Rst = CHotelController.SetHotelRoomType(iHotelCode, iRoomTypeId, ContentText);
                }
                else
                {
                    iRoomTypeId = Convert.ToInt64(cbType.SelectedValue);
                    Rst = CHotelController.NewHotelRoom(iHotelCode, iRoomTypeId, ContentText);
                }
                if (Rst < 0)
                {
                    if (Rst == -2)
                    {
                        iToast.Show("添加信息重复,新建失败!");
                    }
                    else
                    {
                        iToast.Show("添加信息失败!");
                    }
                    return;
                }else
                {
                    iToast.Show("已成功添加!");
                }
                //通知mainwindow刷新房态
                CHotelDelegate.GetHotelEvent().RefreshRoom(iHotelCode); 
                Rst = CHotelController.GetHotelRoomType(out iRoomTypeList, iHotelCode);
                if (Rst < 0)
                {
                    iToast.Show("获取房间分类失败!");
                }
                if (iRoomTypeList != null)
                {
                    InitHotelRoomType();
                }
            }));
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
